<?php
/**
 * 标签列表公共控制器
 * taginfolistcontroller.php
 * 
 * Create on 2015-04-20 15:00:28
 * Create by yuebin
 */
namespace haibao\cms\business\control\pc\lists;


class TagInfoListController extends \haibao\cms\business\control\pc\channel\ChannelController{
	
	const DEFAULT_LIST_PAGE_NUM = 5;
	const NO_STRUCT_TAG = 6;
	const NO_STRUCT_TAG_URL = 'zhuanti';
	private $weekZh = array('星期一','星期二','星期三','星期四','星期五','星期六','星期天');
	private $calendarTag = 0;
	private $siteParentId = 0;
	public function __construct($updateTime = null,$controlId,$isPublish){
		
		parent::__construct($updateTime,$controlId,$isPublish);
	}
	
	
	/**
	 * 获取模版内容
	 * @param : String $content
	 */
	public function displayTemplateHtml(){
		$channelId = 0;
		$controlData = $this->getParameter(self::$CONTROL_DATA);
		$content = $this->getParameter(self::$CONTROL_CONTENT);
		$currentPage = $this->getParameter(self::$CURRENTPAGE);
		$pageable = $this->getParameter(self::$PAGEABLE);
		$siteTemplateId = $this->getParameter(self::$SITE_TEMPLATE_ID);
		$this->replaceAdPosition($controlData['auto']);
		if($controlData['manual']){
			$dataCondition = $controlData['manual'];
			$dataCondition['currentPage'] = $currentPage;
			if(isset($controlData['manual']['tagIds'])) {
				if(isset($controlData['manual']['calendarTag'])){
					$this->calendarTag = $controlData['manual']['calendarTag'];
				}
				$this->parseTagId($controlData['manual']['tagIds'],$controlData['manual']['topTagId']);

				$tagInfo = $this->getParameter(self::$TAG_INFO);
				
				if(is_array($this->conditionTagId)){
					$moreTagId = array_shift($this->conditionTagId);
				}else{
					$moreTagId = $this->conditionTagId;
				}
				$this->assign('moreUrl',\haibao\classlibrary\cms\Config::getConfig(\haibao\classlibrary\cms\Config::CLASSLIBRARY_CONFIG_HOST_PIC).'/piclists/'.$moreTagId.'/');
				$topTagList = self::getTopTags();
				// DO : 根据siteTemplateId 来显示顶部样式
				if($siteTemplateId == \haibao\classlibrary\cms\Config::getConfig(\haibao\classlibrary\cms\Config::TAG_LIST_DIE_TEMPLATE)){
				
					$tagNavArr = array();
					array_push($tagNavArr,array('name'=>'首页','url'=>\haibao\classlibrary\cms\Config::getConfig(\haibao\classlibrary\Config::CLASSLIBRARY_CONFIG_HAIBAO_URL)));
					if($tagInfo->ParentId != self::NO_STRUCT_TAG){
						$parentTagList = \haibao\cms\business\Tag::getParentsTagByIds(array($tagInfo->Id));
						if(isset($parentTagList[$tagInfo->Id])){
							$tempArr = array_reverse($parentTagList[$tagInfo->Id]);
							foreach($tempArr as $temp){
								if($temp->ParentId){
									$url = preg_replace('/\/(.*?)\/(.*)/i', '/${1}/'.$temp->Id.'/', $temp->Url);
									$url = $topTagList[$temp->TopId]['tagchannelurl'].$url;
									array_push($tagNavArr,array('name'=>$temp->TagName,'url'=>$url));
								}else{
									array_push($tagNavArr,array('name'=>$temp->TagName,'url'=>$topTagList[$temp->TopId]['tagchannelurl']));
								}
							}
						}
					}else{
						array_push($tagNavArr,array('name'=>$tagInfo->TagName,'url'=>''));
					}
					$this->assign('showType', 1);
					$this->assign('tagNavArr', $tagNavArr);
				}else{
					$topTagName = $topTagUrl = $logo = '';
					if($controlData['manual']['tagIds'] != $this->topTagId){
						if($this->topTagId != self::NO_STRUCT_TAG){
							if(isset($topTagList[$this->topTagId])){
								$topTagName = $topTagList[$this->topTagId]['name'];
								$topTagUrl = $topTagList[$this->topTagId]['tagchannelurl'];
							}
						}
					}else{
						$topTagUrl = $topTagList[$this->topTagId]['tagchannelurl'];
					}

					if(isset($topTagList[$this->topTagId])) $logo = $topTagList[$this->topTagId]['logo'];
					
					$channelId = $this->getParameter(self::$CHANNEL_ID);
					// DO : 根据频道ID获取二级导航
					$twoNavList = $this->getTwoNav($channelId);
					$this->assign('twoNavNum',count($twoNavList));
					$this->assign('twoNavList', $twoNavList);
					$this->assign('parentId', $this->siteParentId);
					$this->assign('showType', 2);

					$this->assign('topTagName', $topTagName);
					$this->assign('topTagUrl', $topTagUrl);
					$this->assign('logo', $logo);
				}
			};
			$matchDesc = $tagInfo->Desc;
			
			if(!preg_match('/\<p\>([^\s+].*?)\<\/p\>/',$tagInfo->Desc,$matchs)){
				if(!empty($matchs)){
					$matchDesc = '';
				}
			}
			$this->assign('tagName', $tagInfo->TagName);
			$this->assign('desc', $tagInfo->Desc);
			$this->assign('matchDesc', $matchDesc);
			$this->assign('wwwHost', \haibao\classlibrary\cms\Config::getConfig(\haibao\classlibrary\Config::CLASSLIBRARY_CONFIG_HAIBAO_URL));
			
			$this->packageUrl();
			if($this->calendarTag){
				$this->assign('pageUrl',\haibao\classlibrary\cms\Config::getConfig
				(\haibao\classlibrary\Config::CLASSLIBRARY_CONFIG_HOST_CALENDAR).'/list/');
			}
			
			$this->getArticleNewList($dataCondition);
			$this->getArticleHotList($dataCondition);
			$this->getImageCollList($dataCondition);
			
			
			$this->assign('siteTemplateId', $siteTemplateId);
			
			$path = $this->generateHtmlPath(__CLASS__,$this->conditionTagId.$currentPage);
			$this->generateHtml($path,$content);
			$content = $this->getHtml($path);
			$content = $this->replaceCDNLink($content);
			$this->initTag();
			return $content;
		}
	}
	
	/**
	 * 获取最新资讯列表
	 * @param : Array $queryCondition 查询条件
	 */
	private function getArticleNewList($dataCondition){
		$dataCondition['source'] = __CLASS__.$this->conditionTagId;
		$dataCondition['level'] = 1;
		$dataCondition['isProImage'] = true;
		$dataCondition['isTag'] = true;
		$dataCondition['isReturnTag'] = true;
		$dataCondition['isParentTag'] = false;
		$dataCondition['isImageColl'] = false;
		$dataCondition['isCalendarData'] = $this->calendarTag ? true : false;
		$proLevel = $this->calendarTag ? \haibao\cms\model\data\ArticleProImage::Level_SIX : \haibao\cms\model\data\ArticleProImage::Level_FOUR;
		$dataCondition['proImageLevel'] = array($proLevel);
		list($articles,$proImage,$tagInfo,$articleTag,$pager) = $this->getArticleControl(5,$dataCondition);
		$finalArr = $articleIds = array();
		foreach($articles as $key=>$article){
			$temp = array();
			$articleIds[] = $article->Id;
			$temp['Id'] = $article->Id;
			$temp['Title'] = $article->Title;
			$temp['SiteType'] = $article->SiteType;
			if($this->calendarTag){
				$temp['Channel'] = \haibao\classlibrary\cms\Config::getConfig(\haibao\classlibrary\cms\Config::CLASSLIBRARY_CONFIG_HOST_CALENDAR)."/article/"
				.$article->PublishId.".htm";
			}else{
				$temp['Channel'] = self::getArticleUrl($article->PublishId,$articleTag[$article->Id]['ParentId']);
			}
			$temp['Date'] = $this->formatDate($article->PublishTime->format('Y-m-d H:i:s'));
			$temp['EnMonth'] = strtoupper($article->PublishTime->format('M'));
			$temp['DayNum'] = $article->PublishTime->format('j');
			$temp['ZhWeek'] = $this->weekZh[$article->PublishTime->format('N') - 1];

			if(isset($tagInfo[$articleTag[$article->Id]['TagId']])){
				$temp['TagName'] = $tagInfo[$articleTag[$article->Id]['TagId']]['TagName'];
				$temp['TagUrl'] = $tagInfo[$articleTag[$article->Id]['TagId']]['Url'];
			}else{
				$temp['TagUrl'] = $temp['TagName'] = '';
			}



			if(isset($proImage[$article->Id][$proLevel])){
				$temp['ProImage'] = $proImage[$article->Id][$proLevel];
			}else{
				if(isset($proImage[$article->Id][\haibao\cms\model\data\ArticleProImage::Level_ZERO])){
					$temp['ProImage'] = $proImage[$article->Id][\haibao\cms\model\data\ArticleProImage::Level_ZERO];
				}else{
					$temp['ProImage'] = \haibao\cms\business\control\PublicController::getArticleFirstImage($article->Id);
				}
			}
			
			array_push($finalArr,$temp);
				
		}
		if($this->calendarTag){
			$calendarTags = \haibao\cms\business\Calendar::getAttrsByArticleIds($articleIds);
			foreach($finalArr as $key=>$value){
				if(isset($calendarTags[$value['Id']])){
					$finalArr[$key]['TagName'] = $calendarTags[$value['Id']]['TagName'];
					$finalArr[$key]['TagUrl'] = $calendarTags[$value['Id']]['TagUrl'];
				}

			}
		}
		$topList = $bottomList = array();
		if($finalArr){
			
			$topList = array_slice($finalArr, 0,5);
			$bottomList = array_slice($finalArr, 5,10);

		}
		$this->assign('pageCount',$pager->getPageCount());
		$this->assign('pager', $pager);
		$this->assign('topList',$topList);
		$this->assign('bottomList',$bottomList);
	}
	
	/**
	 * 获取最热门资讯列表
	 * @param : Array $queryCondition 查询条件
	 */
	private function getArticleHotList($dataCondition){
	
		$dataCondition['source'] = __CLASS__.$this->conditionTagId;
		$dataCondition['level'] = 1;
		$dataCondition['isProImage'] = true;
		$dataCondition['isTag'] = true;
		$dataCondition['isReturnTag'] = true;
		$dataCondition['isParentTag'] = false;
		$dataCondition['isImageColl'] = false;
		$dataCondition['isDate'] = true;
		$dataCondition['dateType'] = \haibao\cms\business\control\ArticleController::DATE_TYPE_MONTH;

		$proImageLevel = $this->calendarTag ? \haibao\cms\model\data\ArticleProImage::Level_SIX : \haibao\cms\model\data\ArticleProImage::Level_FOUR;
		$dataCondition['proImageLevel'] = array($proImageLevel);
	
		list($articles,$proImage,$tagInfo,$articleTag) = $this->getArticleControl(2,$dataCondition);
		$hotList = array();
		foreach($articles as $key=>$article){
				
			$temp = array();
			$temp['Id'] = $article->Id;
			$temp['Title'] = $article->Title;
			$temp['SiteType'] = $article->SiteType;
			$temp['Channel'] = self::getArticleUrl($article->PublishId,$articleTag[$article->Id]['ParentId']);
			$temp['Date'] = $this->formatDate($article->PublishTime->format('Y-m-d H:i:s'));
				
			if(isset($tagInfo[$articleTag[$article->Id]['TagId']])){
	
				$temp['TagName'] = $tagInfo[$articleTag[$article->Id]['TagId']]['TagName'];
				$temp['TagUrl'] = $tagInfo[$articleTag[$article->Id]['TagId']]['Url'];
			}else{
				$temp['TagUrl'] = $temp['TagName'] = '';
			}

			if(isset($proImage[$article->Id][$proImageLevel])){
				$temp['ProImage'] = $proImage[$article->Id][$proImageLevel];
			}else{
				if(isset($proImage[$article->Id][\haibao\cms\model\data\ArticleProImage::Level_ZERO])){
					$temp['ProImage'] = $proImage[$article->Id][\haibao\cms\model\data\ArticleProImage::Level_ZERO];
				}else{
					$temp['ProImage'] = '';
				}
			}
				
			array_push($hotList,$temp);
	
		}
		$topHotList = $bottomHotList = array();
		if($hotList){
			$topHotList = array_slice($hotList, 0,5);
			$bottomHotList = array_slice($hotList, 5,10);
		
		}
		$this->assign('topHotList',$topHotList);
		$this->assign('bottomHotList',$bottomHotList);
	}
	
	/**
	 * 获取点击量最高的图集
	 */
	public function getImageCollList($dataCondition){
		
		$dataCondition['source'] = __CLASS__.$this->conditionTagId;
		$dataCondition['level'] = 1;
		$dataCondition['limit'] = 4;
		$dataCondition['isProImage'] = true;
		$dataCondition['isTag'] = true;
		$dataCondition['isParentTag'] = false;
		$dataCondition['isDate'] = true;
		$dataCondition['dateType'] = \haibao\cms\business\control\ArticleController::DATE_TYPE_WEEK;
		$dataCondition['proImageLevel'] = array(\haibao\cms\model\data\ArticleProImage::Level_FOUR);
		$dataCondition['orderByNew'] = false;
		
		list($imageColl,$proImage) = $this->getArticleControl(4,$dataCondition);
		$imageCollList = array();
		foreach($imageColl as $key=>$coll){
				
			$temp = array();
			$temp['Id'] = $coll->Id;
			$temp['Title'] = $coll->Title;
			$temp['SiteType'] = $coll->SiteType;
			$temp['Channel'] = self::getArticleUrl($coll->Id,$coll->ImageCollectiveTag->ParentId,2);
				
		
			if(isset($proImage[$coll->Id][\haibao\cms\model\data\ArticleProImage::Level_FOUR])){
				$temp['ProImage'] = $proImage[$coll->Id][\haibao\cms\model\data\ArticleProImage::Level_FOUR];
			}else{
				if(isset($proImage[$coll->Id][\haibao\cms\model\data\ArticleProImage::Level_ZERO])){
					$temp['ProImage'] = $proImage[$coll->Id][\haibao\cms\model\data\ArticleProImage::Level_ZERO];
				}else{
					$temp['ProImage'] = '';
				}
			}
			array_push($imageCollList,$temp);
		}
		$this->assign('imageCollList', $imageCollList);
	}
	
	/**
	 * 封装发布的地址url
	 */
	private function packageUrl(){
		
		$siteChannelTag = $this->getParameter(self::$SITE_CHANNEL_TAG);
		
		$topIds = self::getTopTags();
		$fileName = $this->getParameter(self::$FILE_NAME);
		$siteTemplateId = $this->getParameter(self::$SITE_TEMPLATE_ID);
		
		$this->assign('viewName','publish');
		$topIds = self::getTopTags();
		if(is_array($this->topTagId)){
			
			if($siteTemplateId == \haibao\classlibrary\cms\Config::getConfig(\haibao\classlibrary\cms\Config::TAG_LIST_DIE_TEMPLATE)){

				$channel = $topIds[$this->topTagId[0]]['channel'];
			
			}
			$url = $topIds[$this->topTagId[0]]['tagchannelurl'];
		}else{

			if($siteTemplateId == \haibao\classlibrary\cms\Config::getConfig(\haibao\classlibrary\cms\Config::TAG_LIST_DIE_TEMPLATE)){

				if($this->topTagId != self::NO_STRUCT_TAG){
					$channel = $topIds[$this->topTagId]['channel'];
				}else{
					$channel = self::NO_STRUCT_TAG_URL;
				}
			}
			$url = $topIds[$this->topTagId]['tagchannelurl'];
		}
		
		if($siteTemplateId == \haibao\classlibrary\cms\Config::getConfig(\haibao\classlibrary\cms\Config::TAG_LIST_DIE_TEMPLATE)){

			$url .= '/'.$channel.'/'.$siteChannelTag.'/';
		}else{

			$url .= '/'.trim($fileName,'/').'/';
			$url = preg_replace('/\/channel/i','', $url);
		}
		$this->assign('pageUrl',$url);
	}
	
	/**
	 * 获取二级导航
	 */
	private function getTwoNav($channelId){
		
		$channelInfo = \haibao\cms\business\site\SiteChannel::getSiteChannelById($channelId);
		$twoNavList = array();
		if($channelInfo && $channelInfo->ParentId){
			$parentId = $channelInfo->ParentId;
			if($channelId == 160){
				//品牌秀场走的是时装
				$parentId = 51;
			}
			$this->siteParentId = $parentId;
			$twoNavList = \haibao\cms\business\control\PublicController::getLowerChannelById($parentId);
		}
		return $twoNavList;
	}

}
